//获取应用实例
const app = getApp()
const http = require('../../../utils/httpUtils.js')
const RSA = require("../../../utils/wx_rsa.js");

Page({
    data: {
        navbarData: {
            showCapsule: 1, //是否显示左上角图标
            title: '确认订单' //导航栏 中间的标题
        },
        height: app.globalData.height * 2 + 20,
        address: '' //地址
    },
    onLoad(e) {
        let data = JSON.parse(e.data)
        this.setData({
            payData: data
        })
        this.data.remark = new Map
        let upData = this.data.payData
        http._headerPost('/api/Order/order_affirm', upData, res => {
            if (res.data.code == 1) {
                // console.log(res.data.data)
                this.setData({
                    orderInfo: res.data.data,
                    address: res.data.data.consign
                })
            } else {
                wx.showModal({
                    title: '提示',
                    content: res.data.msg,
                    success: res => {
                        wx.navigateBack({})
                    }
                })
            }
        })
    },
    onShow() {

    },

    //创建订单
    createOrder(e) {
        if (this.data.address == '' || this.data.address == null) {
            wx.showModal({
                title: '提示',
                content: '请先设置您的收货地址！',
                showCancel: false
            })
            return
        }
        //创建订单
        let data = {}
        data.type = this.data.payData.type //下单方式，buyNow|立即购买，cart|购物车，order|待付款订单支付"
        if (data.type == 'buyNow') {
            data.num = RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${this.data.payData.num}`)); // 立即购买时传数量
            data.sku_id = RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${this.data.payData.skuId}`)); //
            data.uc_id = RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${this.data.address.uc_id}`)); //收货地址ID（order时不传，reserve时不传）      
            data.share_uid = RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${this.data.payData.share_uid}`)); //受益人id
        } else if (data.type == 'cart') {
            data.cart_id = RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${this.data.payData.cart_id}`)); // 购物车 ID
            data.uc_id = RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${this.data.address.uc_id}`)) //收货地址ID（order时不传，reserve时不传）      
        } else if (data.type == 'order') {
            // data.order_id = this.data
        }
        data.o_remark = JSON.stringify(this.strMapToObj(this.data.remark))
        data.type = RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(data.type)); //下单类型 type
        data.c_client = RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt("weixinSmall")); //客户端
        // data.v = RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt("1.1.0")); //版本号
        data.v = '1.1.0';

        // console.log(data)
        // 解密测试

        // let dec_rsa = new RSA.RSAKey();
        // dec_rsa = RSA.KEYUTIL.getKey(app.globalData.priKey);
        // let encStr = RSA.b64tohex(data.uc_id);
        // let decStr = dec_rsa.decrypt(encStr)
        // console.log(decStr)

        http._headerPost('/api/Order/commit_order', data, res => {
            // console.log(res)
            if (res.data.code == 1) {
                let payInfo = res.data.data
                wx.navigateTo({
                    url: '../topay/index?payInfo=' + JSON.stringify(res.data.data)
                })
            } else {
                wx.showModal({
                    title: '提示',
                    content: res.data.msg,
                    showCancel: false
                })
            }
        })

        // 配置模板消息推送
        // var postJsonString = {};
        // postJsonString.keyword1 = { value: res.data.data.dateAdd, color: '#173177' }
        // postJsonString.keyword2 = { value: res.data.data.amountReal + '元', color: '#173177' }
        // postJsonString.keyword3 = { value: res.data.data.orderNumber, color: '#173177' }
        // postJsonString.keyword4 = { value: '订单已关闭', color: '#173177' }
        // postJsonString.keyword5 = { value: '您可以重新下单，请在30分钟内完成支付', color: '#173177' }
        // app.sendTempleMsg(res.data.data.id, -1,
        //   'mGVFc31MYNMoR9Z-A9yeVVYLIVGphUVcK2-S2UdZHmg', e.detail.formId,
        //   'pages/index/index', JSON.stringify(postJsonString));
        // postJsonString = {};
        // postJsonString.keyword1 = { value: '您的订单已发货，请注意查收', color: '#173177' }
        // postJsonString.keyword2 = { value: res.data.data.orderNumber, color: '#173177' }
        // postJsonString.keyword3 = { value: res.data.data.dateAdd, color: '#173177' }
        // app.sendTempleMsg(res.data.data.id, 2,
        //   'Arm2aS1rsklRuJSrfz-QVoyUzLVmU2vEMn_HgMxuegw', e.detail.formId,
        //   'pages/order-details/index?id=' + res.data.data.id, JSON.stringify(postJsonString));
        // 下单成功，跳转到订单管理界面
    },
    /**
     * Map转对象
     */
    strMapToObj(strMap) {
        let obj = Object.create(null);
        for (let [k, v] of strMap) {
            obj[k] = v;
        }
        return obj;
    },
    /**
     * 备注
     */
    remarks(e) {
        console.log(e)
        this.data.remark.set(e.currentTarget.id, e.detail.value)
    },
    //添加地址
    addAddress() {
        wx.navigateTo({
            url: "/pages/public/address/address?type=selete&mold=user"
        })
    },
    inputType(e) {
        this.setData({
            Input: e.detail.value
        })
    }
})